import $ from '../common';
Page({
  data: {
    titleHeight: $.gData().titleHeight,
    statuHeight: $.gData().statuHeight,
    isIphoneX: $.gData().isIphoneX,
    info:{},
    defautimg:'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_bt%2F0%2F14087354835%2F1000.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1651388881&t=8d3322dd433dcde1339e81bf8f029364'
  },
  choosimg(e){//放大图片
    const {src}=e.currentTarget.dataset
    const {imgs} = this.data.info.studentpaper
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgs // 需要预览的图片http链接列表
    })
  },
  correcting(){//批改
    const {info}=this.data

    if(!info.studentpaper.correct_memo){
      $.tip('批改内容不能为空')
      return
    }
    if (info.studentpaper.correct_imgs) {
      let a = info.studentpaper.correct_imgs.map((v, i) => { //保存的时候需要将 [{url:'............'},{}] 改成[1,2,3]
        return info.studentpaper[i] = v.url
      });
      info.studentpaper.correct_imgs = a
    }
    $.post('/teacher/testpaper/correct',{
      correct_memo:info.studentpaper.correct_memo,
      correct_imgs:info.studentpaper.correct_imgs,
      testpaper_uuid:info.studentpaper.testpaper_uuid,
      studentpaper_uuid:info.studentpaper.uuid
    },res=>{
      wx.navigateBack({
        delta: -1,
      })
    })
  },
  getinfo() {
    const {studentpaper_uuid}=this.data
    $.get('/testpaper/student/detail',{
      studentpaper_uuid
    },res=>{
      this.setData({
        info:res
      })
      if (res.studentpaper && res.studentpaper.correct_imgs) {
        let a = res.studentpaper.correct_imgs.map(i => {
          return {
            url: i
          }
        })
        this.setData({
          'info.studentpaper.correct_imgs': a
        })
      }
    })
  },
  texthand(e) {//文本域的输入
    this.setData({
      'info.studentpaper.correct_memo': e.detail.value
    })  },
  afterRead(event) { //上传图片
    const {
      file
    } = event.detail;
    const {
      info
    } = this.data
    let files = file.map(v => { //因为vant 返回的解构是[{url:'............'}] $.uploadFileList 需要的结构式[1,2,3]
      return v.url
    })
    $.uploadFileList("/image/upload/oss", {
      prefix: "keday/",
      fileList: files
    }, res => {
      let a = res.map(i => { //返回的结构式[1,2,3,5] 需要改成vant需要的[{url:'............'},{}]
        return {
          url: i
        }
      })
      let arr //判断之前有没有 图片 有的话合并
      if (info.studentpaper) {
        if (info.studentpaper.correct_imgs) {
          arr = [...info.studentpaper.correct_imgs, ...a]
        } else {
          arr = [...a]
        }
      } else {
        info.studentpaper = {}
        arr = [...a]
        info.studentpaper.correct_imgs = arr
      }
      this.setData({
        'info.studentpaper.correct_imgs': arr
      })
    })
  },
  imgdelete(event) { //删除图片
    console.log('删除');
    const {info} = this.data
    info.studentpaper.correct_imgs.splice(event.detail.index, 1)
    this.setData({
      info
    })
  },
  onLoad(e) {
    const {studentpaper_uuid}=e
    this.setData({
      studentpaper_uuid
    })
    this.getinfo()
  },
  onShow() {}
})